package org.zzz.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.zzz.entity.Course;
import org.zzz.entity.TeacherCourse;
import org.zzz.entity.vo.CourseShowVo;
import org.zzz.mapper.CourseMapper;
import org.zzz.mapper.TeacherMapper;
import org.zzz.service.CourseService;
import org.zzz.service.TeacherCourseService;
import org.zzz.service.TeacherService;
import org.zzz.utils.MybatisUtil;

import java.util.List;

/**
 * @Author ZZF
 * @Time 2021/04/20 22:46
 */
public class CourseServiceImpl implements CourseService {
    private CourseMapper courseMapper;
    private TeacherCourseService teacherCourseService = new TeacherCourseServiceImpl();
    private TeacherMapper teacherMapper;

    {
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
        courseMapper = MybatisUtil.getMapper(CourseMapper.class);
        teacherMapper = MybatisUtil.getMapper(TeacherMapper.class);
    }

    @Override
    public List<Course> listCourse() {
        return courseMapper.listCourse();
    }

    @Override
    public Course getCourseById(String id) {
        return courseMapper.getCourseById(id);
    }

    @Override
    public boolean insertCourse(Course course) {
        return courseMapper.insertCourse(course) > 0;
    }

    @Override
    public boolean updateCourse(Course course) {
        return courseMapper.updateCourse(course) > 0;
    }

    @Override
    public boolean deleteCourseById(String id) {
        return courseMapper.deleteCourseById(id) > 0;
    }

    @Override
    public List<CourseShowVo> listCourseShowVo() {
        List<CourseShowVo> courseShowVos = courseMapper.listCourseShowVo();
        for (CourseShowVo courseShowVo : courseShowVos) {
            TeacherCourse tcByCid = teacherCourseService.getTCByCid(courseShowVo.getId());
            String teacherName = "";
            if (tcByCid!=null){
                //如果查到就查询教师名称
                teacherName = teacherMapper.getTeacherNameById(tcByCid.getTid());
            }
            courseShowVo.setTeacherName(teacherName);
        }
        return courseShowVos;
    }

    @Override
    public CourseShowVo getCourseShowVo(String type, String value) {
        if ("cid".equals(type)){
            CourseShowVo courseShowVo = courseMapper.getCourseShowVoByCid(value);
            TeacherCourse tcByCid = teacherCourseService.getTCByCid(courseShowVo.getId());
            String teacherName = "";
            if (tcByCid!=null){
                //如果查到就查询教师名称
                teacherName = teacherMapper.getTeacherNameById(tcByCid.getTid());
            }
            courseShowVo.setTeacherName(teacherName);
            return courseShowVo;
        }else if ("cname".equals(type)){
            CourseShowVo courseShowVo = courseMapper.getCourseShowVoByCname(value);
            TeacherCourse tcByCid = teacherCourseService.getTCByCid(courseShowVo.getId());
            String teacherName = "";
            if (tcByCid!=null){
                //如果查到就查询教师名称
                teacherName = teacherMapper.getTeacherNameById(tcByCid.getTid());
            }
            courseShowVo.setTeacherName(teacherName);
            return courseShowVo;
        }else{
            return null;
        }

    }
}
